package pl.edu.pw.polygen.menu.commands;


import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import pl.edu.pw.polygen.MainWindow;
import pl.edu.pw.polygen.PolygenApplication;
import pl.edu.pw.polygen.beans.FileBean;
import pl.edu.pw.polygen.core.file.FileService;
import pl.edu.pw.polygen.modeler.server.EditorServer;
import pl.edu.pw.polygen.util.ApplicationHelper;
import pl.edu.pw.polygen.util.ApplicationHolder;

import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;

@Component
public class SaveCommand implements Command, Observer {
	

	private static final long serialVersionUID = 7875336550119938214L;
	
	@Autowired
	private FileService fileService;
	
	@Override
	public void menuSelected(MenuItem selectedItem) {
		System.out.println(selectedItem.getText());
		MainWindow mainWindow = (MainWindow) ((PolygenApplication)ApplicationHolder.getApplication()).getMainWindow();
		EditorServer editorServer = mainWindow.getSelectedEditorServer();
		editorServer.getGraphData();
	}

	@Override
	public void update(Observable o, Object arg) {
		MainWindow mainWindow = (MainWindow) ((PolygenApplication)ApplicationHolder.getApplication()).getMainWindow();
		EditorServer editorServer = mainWindow.getSelectedEditorServer();
		String polygenData = editorServer.getPolygenData();
		FileBean fileBean = mainWindow.getSelectedTab();
		fileBean.setContent(polygenData);
		fileBean.setModificationDate(new Date());
		fileService.saveFile(fileBean);
		ApplicationHelper.showNotification("Save file", "File '" + fileBean.getFileName() +"' has been saved.");
	}

}
